package analysis;

import common.AnalysisWare;
import common.Constants;
import common.DataAnalysis;
import redis.clients.jedis.Tuple;
import util.FileUtil;
import util.remote.RemoteConnectResult;
import util.remote.RemoteType;

import java.util.Map;
import java.util.Set;

/**
 * Created by J.Chen on 2017/5/31.
 */
@AnalysisWare(confCode = "LOVE_HOUSE_DATA", desc = "小窝上周周榜查询", remote = {RemoteType.qq, RemoteType.qk})
public class LoveHouseDataAnalysis extends DataAnalysis {
    public LoveHouseDataAnalysis(RemoteConnectResult result, String confCode) {
        super(result, confCode);
    }

    @Override
    public void execute() {
        Set<Tuple> set = result.getRedisUtil().zrevrangeWithScores(getWeekLoveRankKey(Constants.getLastMonday()), 0, 20);
        FileUtil fileUtil = new FileUtil(Constants.getActOutputDir(result.getRemoteConf(), "loveHouse", "" + Constants.getLastMonday()));
        fileUtil.writeln("|", "小窝id", "上周增长的恩爱值", "开通人id", "配偶id", "总的恩爱值");
        for (Tuple t : set) {
            Map<String, String> map = result.getMysqlUtil().selectOne(String.format("select * from love_house_info where houseId = %s", t.getElement()));
            fileUtil.writeln("|", t.getElement(), "" + t.getScore(), map.get("masterId"), map.get("spouseId"), map.get("fullNumLove"));
        }
        fileUtil.close();

    }

    private String getWeekLoveRankKey(int weekIndex) {
        return String.join(":", "z" + "LoveHouse", "loveRank", "week", "" + weekIndex);
    }

    private String getLoveHouseRedisKey(String houseId) {
        return String.join(":", "h" + "LoveHouse", "info", "" + houseId);
    }
}
